package com.asohofrucol.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.asohofrucol.dao.DealTechnicalAssistanceDao;
import com.asohofrucol.dao.DealTechnicalAssistanceVisitDao;
import com.asohofrucol.dto.DealAssistenceRegisterDTO;
import com.asohofrucol.dto.DealTechnicalAssistanceDTO;
import com.asohofrucol.dto.DealTechnicalAssistanceVisitDTO;
import com.asohofrucol.dto.util.DealTechnicalAssistanceHelperDto;
import com.asohofrucol.dto.util.DealTechnicalAssistanceVisitHelperDto;
import com.asohofrucol.model.DealTechnicalAssistance;
import com.asohofrucol.model.DealTechnicalAssistanceVisit;
import com.asohofrucol.service.DealTechnicalAssistanceService;

@Component
public class DealTechnicalAssistanceServiceImpl implements
		DealTechnicalAssistanceService {
	
	@Autowired
	DealTechnicalAssistanceDao dealTechnicalAssistanceDao;
	
	@Autowired
	DealTechnicalAssistanceVisitDao dealTechnicalAssistanceVisitDao;		

	@Override
	public DealTechnicalAssistanceDTO getDealTechnicalAssistanceByRuatId(
			Long ruatId) {
		List<DealTechnicalAssistance> ent = dealTechnicalAssistanceDao.listByRuatId(ruatId);
		if(ent.isEmpty()){
			return null;
		}
		return DealTechnicalAssistanceHelperDto.convertToRegisterDTO(ent.get(0));
	}

	@Override
	public List<DealTechnicalAssistanceVisitDTO> getDealTechnicalAssistanceVisitsByDealTechnicalAssistanceId(
			Long tavId) {
		List<DealTechnicalAssistanceVisit> ents = dealTechnicalAssistanceVisitDao.listByDealTechnicalAssistance(tavId);
		List<DealTechnicalAssistanceVisitDTO> dtos = new ArrayList<DealTechnicalAssistanceVisitDTO>();
		for(int pos =0; pos<ents.size();pos++){
			DealTechnicalAssistanceVisit ent = ents.get(pos);
			DealTechnicalAssistanceVisitDTO dto = DealTechnicalAssistanceVisitHelperDto.convertToRegisterDTO(ent);
			dto.setPosition(pos+1);
			dtos.add(dto);
		}
		return dtos;
	}

	@Override
	public void saveDealAssistenceRegister(DealAssistenceRegisterDTO dealReg, String user) {
		DealTechnicalAssistanceDTO tasDto = dealReg.getDealTechnicalAssistance();
		List<DealTechnicalAssistanceVisitDTO>  lisvis = dealReg.getDealTechnicalAssistanceVisits();
		DealTechnicalAssistance tasEnt = DealTechnicalAssistanceHelperDto.converRegisterToEntity(tasDto);
		tasEnt = dealTechnicalAssistanceDao.saveAndLog(tasEnt, user);
		Long id_tas = tasEnt.getId();
		List<DealTechnicalAssistanceVisit> lient = new ArrayList<DealTechnicalAssistanceVisit>();
		for(DealTechnicalAssistanceVisitDTO visit:lisvis){
			visit.setDealTechnicalAssistanceId(id_tas);
			lient.add(DealTechnicalAssistanceVisitHelperDto.converRegisterToEntity(visit));
		}
		dealTechnicalAssistanceVisitDao.saveAndLog(lient, user);
	}
	
	@Override
	public void saveDealAssistence(DealTechnicalAssistanceDTO dealReg, String user) {
		DealTechnicalAssistanceDTO tasDto = dealReg;
		DealTechnicalAssistance tasEnt = DealTechnicalAssistanceHelperDto.converRegisterToEntity(tasDto);
		tasEnt = dealTechnicalAssistanceDao.saveAndLog(tasEnt, user);
	}
	
	@Override
	public void saveDealAssistenceVisit(DealTechnicalAssistanceVisitDTO dealReg, String user) {
		DealTechnicalAssistanceVisit ent = DealTechnicalAssistanceVisitHelperDto.converRegisterToEntity(dealReg);
		dealTechnicalAssistanceVisitDao.saveAndLog(ent, user);
	}

	@Override
	public void saveEvidenceFileName(Long dealAssistenceVisitId,
			String fileName, String user) {
		dealTechnicalAssistanceVisitDao.saveEvidenceFileName(dealAssistenceVisitId, fileName);
	}
	
	
}
